package com.usian.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.usian.config.RedisClient;
import com.usian.mapper.TbContentCategoryMapper;
import com.usian.mapper.TbContentMapper;
import com.usian.pojo.TbContent;
import com.usian.pojo.TbContentCategory;
import com.usian.pojo.TbContentCategoryExample;
import com.usian.pojo.TbContentExample;
import com.usian.service.ContentService;
import com.usian.utils.AdNode;
import com.usian.utils.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class ContentServiceImpl implements ContentService {

    @Value("${AD_CATEGORY_ID}")
    private Long AD_CATEGORY_ID;

    @Value("${AD_HEIGHT}")
    private Integer AD_HEIGHT;

    @Value("${AD_WIDTH}")
    private Integer AD_WIDTH;

    @Value("${AD_HEIGHTB}")
    private Integer AD_HEIGHTB;

    @Value("${AD_WIDTHB}")
    private Integer AD_WIDTHB;

    @Autowired
    private RedisClient redisClient;


    @Autowired
    private TbContentCategoryMapper tbContentCategoryMapper;

    @Autowired
    private TbContentMapper tbContentMapper;

    /**
     * 根据id查询ContentCategory对象
     * @param id
     * @return
     */
    @Override
    public List<TbContentCategory> selectContentCategoryByParentId(Long id) {
        TbContentCategoryExample example = new TbContentCategoryExample();
        TbContentCategoryExample.Criteria criteria = example.createCriteria();
        criteria.andParentIdEqualTo(id);
        criteria.andStatusEqualTo(1);

        return tbContentCategoryMapper.selectByExample(example);
    }

    /**
     * 添加ContentCategory
     * @param parentId
     * @param name
     * @return
     */
    @Override
    public Boolean insertContentCategory(Long parentId, String name) {
        TbContentCategory tbContentCategory = new TbContentCategory();
        tbContentCategory.setCreated(new Date());
        tbContentCategory.setUpdated(new Date());
        tbContentCategory.setParentId(parentId);
        tbContentCategory.setName(name);
        tbContentCategory.setSortOrder(1);
        tbContentCategory.setIsParent(false);
        int insertOk = tbContentCategoryMapper.insertSelective(tbContentCategory);


        TbContentCategory tbContentCategory1 = new TbContentCategory();
        tbContentCategory1.setIsParent(true);
        tbContentCategory1.setId(parentId);
        tbContentCategoryMapper.updateByPrimaryKeySelective(tbContentCategory1);

        if (insertOk==1){
            return true;
        }
        return false;
    }

    /**
     * 递归删除
     * 逻辑删除ContentCategory对象并判断父级目录是否还拥有子目录
     * @param categoryId
     * @return
     */
    @Override
    public void deleteContentCategoryById(Long categoryId) {
        /* 查询出要删除的对象 */
        TbContentCategory tbContentCategory = tbContentCategoryMapper.selectByPrimaryKey(categoryId);

        /* 判断是否为父级目录 */
        if (tbContentCategory.getIsParent()){
            /* 查询出来所有的子集目录 */
            TbContentCategoryExample example = new TbContentCategoryExample();
            TbContentCategoryExample.Criteria criteria = example.createCriteria();
            criteria.andParentIdEqualTo(tbContentCategory.getId());
            List<TbContentCategory> tbContentCategories = tbContentCategoryMapper.selectByExample(example);

            /* 修改目录状态及逻辑删除目录 */
            TbContentCategory tbContentCategory1 = new TbContentCategory();
            tbContentCategory1.setId(tbContentCategory.getId());
            tbContentCategory1.setStatus(2);
            tbContentCategory1.setIsParent(false);
            tbContentCategory1.setUpdated(new Date());
            tbContentCategoryMapper.updateByPrimaryKeySelective(tbContentCategory1);

            /* 根据删除的对象判断父级目录是否还有子目录 */
            TbContentCategoryExample example1 = new TbContentCategoryExample();
            TbContentCategoryExample.Criteria criteria1 = example1.createCriteria();
            criteria1.andParentIdEqualTo(tbContentCategory.getParentId());
            criteria1.andStatusEqualTo(1);
            List<TbContentCategory> tbContentCategories2 = tbContentCategoryMapper.selectByExample(example1);
            if (tbContentCategories2.size()==0){
                /* 没有查到证明父级目录下没有子目录 修改父级目录状态 */
                TbContentCategory tbContentCategory2 = new TbContentCategory();
                tbContentCategory2.setId(tbContentCategory.getParentId());
                tbContentCategory2.setIsParent(false);
                tbContentCategory2.setUpdated(new Date());
                tbContentCategoryMapper.updateByPrimaryKeySelective(tbContentCategory2);
            }

            /* 循环递归判断 */
            for (TbContentCategory contentCategory : tbContentCategories) {
                deleteContentCategoryById(contentCategory.getId());
            }
        }else {
            /* 逻辑删除 */
            tbContentCategory.setUpdated(new Date());
            tbContentCategory.setStatus(2);
            int deleteStatus = tbContentCategoryMapper.updateByPrimaryKeySelective(tbContentCategory);
            /*物理删除
            int deleteStatus = tbContentCategoryMapper.deleteByPrimaryKey(categoryId);*/

            /* 不是父级目录的话删除子目录下的广告 */
            TbContentExample tbContentExample = new TbContentExample();
            TbContentExample.Criteria criteria1 = tbContentExample.createCriteria();
            criteria1.andCategoryIdEqualTo(tbContentCategory.getId());
            tbContentMapper.deleteByExample(tbContentExample);

            /* 根据删除的对象判断父级目录是否还有子目录 */
            TbContentCategoryExample example = new TbContentCategoryExample();
            TbContentCategoryExample.Criteria criteria = example.createCriteria();
            criteria.andParentIdEqualTo(tbContentCategory.getParentId());
            criteria.andStatusEqualTo(1);
            List<TbContentCategory> tbContentCategories = tbContentCategoryMapper.selectByExample(example);
            if (tbContentCategories.size()==0){
                /* 没有查到证明父级目录下没有子目录 修改父级目录状态 */
                TbContentCategory tbContentCategory1 = new TbContentCategory();
                tbContentCategory1.setId(tbContentCategory.getParentId());
                tbContentCategory1.setIsParent(false);
                tbContentCategory1.setUpdated(new Date());
                tbContentCategoryMapper.updateByPrimaryKeySelective(tbContentCategory1);
            }
        }
    }


    /**
     * 修改
     * @param id
     * @param name
     * @return
     */
    @Override
    public Boolean updateContentCategory(Long id, String name) {
        TbContentCategory tbContentCategory = new TbContentCategory();
        tbContentCategory.setUpdated(new Date());
        tbContentCategory.setId(id);
        tbContentCategory.setName(name);
        int updateOk = tbContentCategoryMapper.updateByPrimaryKeySelective(tbContentCategory);
        if (updateOk==1){
            return true;
        }
        return false;
    }

    @Override
    public PageResult selectTbContentAllByCategoryId(Long categoryId) {
        PageHelper.startPage(1,10);
        TbContentExample example = new TbContentExample();
        TbContentExample.Criteria criteria = example.createCriteria();
        criteria.andCategoryIdEqualTo(categoryId);
        List<TbContent> list = tbContentMapper.selectByExampleWithBLOBs(example);
        PageInfo<TbContent> pageInfo = new PageInfo<>(list);
        PageResult pageResult = new PageResult();
        pageResult.setResult(list);
        pageResult.setPageIndex(pageInfo.getPageNum());
        pageResult.setTotalPage(pageInfo.getPages());
        return pageResult;
    }

    @Override
    public Boolean insertTbContent(TbContent tbContent) {
        int i = tbContentMapper.insertSelective(tbContent);
        if (i==1){
            return true;
        }
        return false;
    }

    @Override
    public void deleteContentByIds(Integer[] ids) {
        for (Integer id : ids) {
            tbContentMapper.deleteByPrimaryKey((long)id);
        }
    }

    @Override
    public List<AdNode> selectFrontendContentByAD() {

        /* 首先先从redis中查询 */
        List<AdNode> list1 = (List<AdNode>) redisClient.hget("daguanggao","89");
        if (list1!=null){
            return list1;
        }

        // 查询TbContent
        TbContentExample tbContentExample = new TbContentExample();
        TbContentExample.Criteria criteria = tbContentExample.createCriteria();
        criteria.andCategoryIdEqualTo(AD_CATEGORY_ID);
        List<TbContent> tbContentList =
                tbContentMapper.selectByExample(tbContentExample);
        List<AdNode> adNodeList = new ArrayList<AdNode>();
        for (TbContent tbContent : tbContentList) {
            AdNode adNode = new AdNode();
            adNode.setSrc(tbContent.getPic());
            adNode.setSrcB(tbContent.getPic2());
            adNode.setHref(tbContent.getUrl());
            adNode.setHeight(AD_HEIGHT);
            adNode.setWidth(AD_WIDTH);
            adNode.setHeightB(AD_HEIGHTB);
            adNode.setWidthB(AD_WIDTHB);
            adNodeList.add(adNode);
        }

        /* 保存到redis中 */
        redisClient.hset("daguanggao","89",adNodeList);
        return adNodeList;
    }
}
